﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：吕海乐 
    //
    // 文件名：ProcureCatalogList.cs
    // 文件功能描述：产品库查询
    
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
// 审核时间：
// 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.Model.Enum;
using System.Data;

namespace HZ.DrugTrade.Web.Admin.DrugLibManage
{
    public partial class CheckProcureCatalogList : BasePage
    {
        public CheckProcureCatalogList()
        {
            this.ModuleNo = Module.产品信息审核.ToString();
            this.PageOperateType = OperateType.产品信息审核.ToString();
        }

        IBLL_ProcurementBasic pbCtrl = Factory.BLL_ProcurementBasic();
        IBLL_ProcurementBasic ipb = Factory.BLL_ProcurementBasic();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //绑定所有项目
                //ProcurementProject procurementProject = new ProcurementProject();
                //procurementProject.ProcureStartTimeFrom = Convert.ToDateTime("1890-01-01");
                //procurementProject.ProcureStartTimeTo = Convert.ToDateTime("2099-12-31");
                //procurementProject.ProcureEndTimeFrom = Convert.ToDateTime("2000-01-01");
                //procurementProject.ProcureEndTimeTo = Convert.ToDateTime("2099-12-31");
                //int temp;
                //IList<ProcurementProject> projectList = ipb.GetProjectByWhere(procurementProject, 1, 0, out temp);
                //ddlProject.DataSource = projectList;
                //ddlProject.DataTextField = "PROJECTNAME";
                //ddlProject.DataValueField = "PROJECTID";
                //ddlProject.DataBind();
                //ListItem item = new ListItem();
                //item.Text = Constants.ALL_ITEM_TEXT;
                //item.Value = Constants.ALL_ITEM_VALUE;
                //ddlProject.Items.Insert(0, item);
                setBind();
            }
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            setBind();
        }

        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            setBind();
        }

        /// <summary>
        /// 根据查询条件绑定采购目录
        /// </summary>
        private void setBind()
        {
            //获取查询条件
            Model.Query.ProcureCatalog procureCatalog = new HZ.DrugTrade.Model.Query.ProcureCatalog();
            procureCatalog.GoodsID = txtGoodsID.Text.Trim();
            procureCatalog.CompanyName_SC = txtProduceCompany_SC.Text.Trim();
            //procureCatalog.PinyinCode = this.txtPinyinCode.Text.Trim().ToUpper();
            procureCatalog.PinyinCode = this.txtpinyincode.Text.Trim().ToUpper();
            procureCatalog.Outlookc = txtOutLook.Text.Trim();
            procureCatalog.Material = txtMaterial.Text.Trim();
            //procureCatalog.ProjectID = ddlProject.SelectedItem.Value;
            procureCatalog.AUDITUSERNAME = txtAuditUserName.Text.Trim(); //审核人
            procureCatalog.ProjectID = this.ddlProjectid.SelectedValue;
            procureCatalog.Unit = txtUnit.Text.Trim();
            procureCatalog.GoodsName = txtGoodsName.Text.Trim();
            procureCatalog.IsUsing = true;
            procureCatalog.CompanyName_TB = txtCompany_TB.Text;
            //procureCatalog.SortName = txtProductType.Text;
            procureCatalog.GoodsName = txtGoodsName.Text.Trim();
            procureCatalog.SortFields = " CompanyName_TB asc,ProcureCataLogID";

            procureCatalog.Append = " nvl(SubmitStatus,0) " + (ddlSubmitStatus.SelectedItem.Value == "1" ? "=1" : ddlSubmitStatus.SelectedItem.Value == "0" ? "=0" : " in (0,1)") + " and nvl(CheckStatus,0) " + (ddlCheckstatus.SelectedItem.Value == "1" ? "=1" : ddlCheckstatus.SelectedItem.Value == "2" ? "=2" : ddlCheckstatus.SelectedItem.Value == "0" ? "=0" : " in (0,1,2)");

            int totalCount = 0;
            IBLL_ProcurementBasic ipb = Factory.BLL_ProcurementBasic();
            try
            {
                IList<Model.View.ProcureCatalog> procureCatalogList = ipb.GetProcureCatalogByWhere(procureCatalog, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalCount);
                AspNetPager1.RecordCount = totalCount;
                gvwProduceCatalog.DataSource = procureCatalogList;
                gvwProduceCatalog.DataBind();

            }
            catch (Exception ex)
            {
                PageError(ex.Message, "");
            }
        }

        /// <summary>
        /// 简单查询与高级查询间的转换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbnSwitch_Click(object sender, EventArgs e)
        {
            if (lbnSwitch.CommandArgument == "simple")
            {
                ltlAdvance.Text = "";
                lbnSwitch.CommandArgument = "advance";
                lbnSwitch.Text = "简单查询";
            }
            else
            {
                ltlAdvance.Text = "<tbody id='advance' style='display:none'>";
                lbnSwitch.CommandArgument = "simple";
                lbnSwitch.Text = "高级查询";

                //清空数据
                txtGoodsID.Text = "";
                txtCompany_TB.Text = "";
                //txtPinyinCode.Text = "";
                txtProduceCompany_SC.Text = "";
                txtAuditUserName.Text = "";
                //txtProductType.Text = "";
                txtUnit.Text = "";
                txtMaterial.Text = "";
                txtGoodsName.Text = "";
                //ddlProject.SelectedIndex = -1;
            }
        }
        protected void gvwProduceCatalog_Command(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Update")
            {
                string id = e.CommandArgument.ToString();
                Response.Redirect("/Enterprise/DrugLibQuery/UpdateProcureCatalog.aspx?GoodsId=" + id);
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            int flag = 0;
            for (int i = 0; i < gvwProduceCatalog.Rows.Count; i++)
            {
                CheckBox check = gvwProduceCatalog.Rows[i].FindControl("check") as CheckBox;
                if (check.Checked)
                {
                    string GoodsId = gvwProduceCatalog.DataKeys[i].Value.ToString();

                    flag += ipb.SubmitProcureCatalogList(GoodsId, 1, "");
                }
            }
            if (flag > 0)
            {
                HZ.Web.WebUtil.Alert("审核成功！");
                setBind();
            }
            else
            {
                HZ.Web.WebUtil.Alert("至少选择一项！");
                setBind();
            }
        }
        public string GetCheck(string flag)
        {
            if (flag == "1")
            {
                return "<font color=green>审核通过</font>";
            }
            if (flag == "2")
            {
                return "<font color=red>审核不通过</font>";
            }
            else
            {
                return "未审核";
            }
        }
        protected void btnCheck_Click(object sender, EventArgs e)
        {
            int flag = 0;
            for (int i = 0; i < gvwProduceCatalog.Rows.Count; i++)
            {
                CheckBox check = gvwProduceCatalog.Rows[i].FindControl("check") as CheckBox;
                if (check.Checked)
                {

                    string GoodsId = gvwProduceCatalog.DataKeys[i].Value.ToString();
                    TextBox txtNoPass = gvwProduceCatalog.Rows[i].FindControl("txtNopassreson") as TextBox; //审核不通过原因
                    if (string.IsNullOrEmpty(HZ.Fun2.CheckString(txtNoPass.Text.Trim())))
                    {
                        HZ.Web.WebUtil.Alert("第" + (i + 1) + "行请填写审核不通过原因！");
                        return;
                    }
                    flag += ipb.SubmitProcureCatalogList(GoodsId, 2, HZ.Fun.CheckString(txtNoPass.Text.Trim()));
                }
            }
            if (flag > 0)
            {
                HZ.Web.WebUtil.Alert("审核成功！");
                setBind();
            }
            else
            {
                HZ.Web.WebUtil.Alert("至少选择一项！");
                setBind();
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            //获取查询条件
            Model.Query.ProcureCatalog procureCatalog = new HZ.DrugTrade.Model.Query.ProcureCatalog();
            procureCatalog.GoodsID = txtGoodsID.Text.Trim();
            procureCatalog.CompanyName_SC = txtProduceCompany_SC.Text.Trim();
            procureCatalog.Outlookc = txtOutLook.Text.Trim();
            procureCatalog.Material = txtMaterial.Text.Trim();
            //procureCatalog.ProjectID = ddlProject.SelectedItem.Value;
            procureCatalog.AUDITUSERNAME = txtAuditUserName.Text.Trim();  //审核人 
            procureCatalog.Unit = txtUnit.Text.Trim();
            procureCatalog.GoodsName = txtGoodsName.Text.Trim();
            procureCatalog.IsUsing = true;
            procureCatalog.CompanyName_TB = txtCompany_TB.Text;
            //procureCatalog.SortName = txtProductType.Text;
            procureCatalog.GoodsName = txtGoodsName.Text.Trim();
            procureCatalog.PinyinCode = this.txtpinyincode.Text.Trim().ToUpper();
            procureCatalog.SortFields = " CompanyName_TB asc,ProcureCataLogID";
            procureCatalog.ProjectID = this.ddlProjectid.SelectedValue;
            procureCatalog.Append = " nvl(SubmitStatus,0) " + (ddlSubmitStatus.SelectedItem.Value == "1" ? "=1" : ddlSubmitStatus.SelectedItem.Value == "0" ? "=0" : " in (0,1)") + " and nvl(CheckStatus,0) " + (ddlCheckstatus.SelectedItem.Value == "1" ? "=1" : ddlCheckstatus.SelectedItem.Value == "2" ? "=2" : ddlCheckstatus.SelectedItem.Value == "0" ? "=0" : " in (0,1,2)");

            int totalCount = 0;
            //IBLL_ProcurementBasic ipb = Factory.BLL_ProcurementBasic();
            try
            {
                //DataTable dt = ipb.GetProcureCatalogByWhere1(procureCatalog, 1, 0, out totalCount);
                ////DataTable dt = HZ.Data.DAL_Helper.GetDataTableFromModel(procureCatalogList);
                //HZ.Excel.ExcelHelper.ExportDataForWeb(dt, gvForExport);
                string where = HZ.DrugTrade.DAL2.DAL_Helper.GetWhereCondition<HZ.DrugTrade.Model.Query.ProcureCatalog>(procureCatalog, true);
                string sql = string.Format(@"SELECT GoodsId 产品编号,GoodsName 产品名称,Outlookc 规格型号,Material 材质,Unit 最小计量单位,bidprice 入围价格,
                                            PackCount 最小零售包装数量,case when packcount is null then ''else to_char(to_number(packcount)*bidprice) end as 最小零售包装价格,PackUnit 包装单位,QualityLevel 产品类别, CompanyName_SC 生产企业,
                                            CompanyName_TB 投标企业,case when SubmitStatus=1 then '已提交' else '未提交'end as 提交状态,
                                            case when CheckStatus=1 then '审核通过' when CheckStatus=0 then '未审核' when CheckStatus=2 then '审核未通过'end as 审核状态,
                                            NOPASSRESON 不通过原因,auditusername 审核人 from View_ProcureCatalog_tb WHERE 1=1 {0}", where);
                HZ.DrugTrade.BLL.ExcelHelper.ExportByWeb(HZ.Data.DbHelperFactory.Create().ExecuteTable(sql), "", "产品审核信息.xls");
            }
            catch (System.Threading.ThreadAbortException ex)
            {
                throw ex;
            }

        }

        protected void gvwProduceCatalog_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                if (ddlProjectid.SelectedItem.Value == "0002")
                {
                    e.Row.Cells[6].Text = "自主报价";
                }
                else
                {
                    e.Row.Cells[6].Text = "采购限价";
                }
                gvwProduceCatalog.Columns[8].Visible = false;
            }
        }
    }
}
